using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Core;

namespace SPHelpCollectionBuilder
{
    public partial class ParentHelpItemChooser : Form
    {
        private bool _hasChanges = false;

        public bool HasChanges
        {
            get { return _hasChanges; }
            set { _hasChanges = value; }
        }

        private HelpItem _currentSelectedHelpItem = null;

        private List<HelpItemManifestEntryParent> _parentEntries = null;

        public List<HelpItemManifestEntryParent> ParentEntries
        {
            get { return _parentEntries; }
            set { _parentEntries = value; }
        }

        public ParentHelpItemChooser()
        {
            InitializeComponent();
        }

        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (ParentEntries == null && listBoxParentHelpItem.Items.Count > 0)
                ParentEntries = new List<HelpItemManifestEntryParent>();

            foreach (object item in listBoxParentHelpItem.Items)
            {
                ParentEntries.Add((HelpItemManifestEntryParent)item);
            }

            this.DialogResult = DialogResult.OK;
        }

        private void textBoxSortOrder_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!char.IsDigit(e.KeyChar) && !char.IsControl(e.KeyChar))
            {
                e.Handled = true;
            }  
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            if (_currentSelectedHelpItem == null)
            {
                _currentSelectedHelpItem = null;
                MessageBox.Show("Please choose a parent help item");
                return;
            }

            if (string.IsNullOrEmpty(textBoxSortOrder.Text))
            {
                _currentSelectedHelpItem = null;
                MessageBox.Show("Please choose sort order");
                return;
            }

            int sortNumer = int.Parse(textBoxSortOrder.Text);

            HelpItemManifestEntryParent parent = new HelpItemManifestEntryParent();
            parent.primary = checkBoxPrimary.Checked;
            parent.sortOrder = sortNumer;
            parent.Value = _currentSelectedHelpItem.id;

            listBoxParentHelpItem.Items.Add(parent);

            ClearControls();
            this.HasChanges = true;
        }

        private void ClearControls()
        {
            _currentSelectedHelpItem = null;
            textBoxSortOrder.Text = string.Empty;
            checkBoxPrimary.Checked = false;
        }

        private void buttonSelectHelpItem_Click_1(object sender, EventArgs e)
        {
            using (TreeViewPicker picker = new TreeViewPicker())
            {
                DialogResult dResult = picker.ShowDialog(this);
                if (dResult == DialogResult.OK)
                {
                    _currentSelectedHelpItem = (HelpItem)picker.SelectedTreeNodeTag.MirrorObject;
                    lblSelectedHelpItem.Text = _currentSelectedHelpItem.id;
                    lblSelectedHelpItem.ForeColor = System.Drawing.Color.Green;
                }
            }
        }

        private void listBoxParentHelpItem_SelectedIndexChanged(object sender, EventArgs e)
        {
            btnRemove.Enabled = true;
            HelpItemManifestEntryParent selectedItem = (HelpItemManifestEntryParent)listBoxParentHelpItem.SelectedItem;
            lblSelectedHelpItem.Text = selectedItem.Value;
            lblSelectedHelpItem.ForeColor = System.Drawing.Color.Green;
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            this.HasChanges = true;
            if(listBoxParentHelpItem.SelectedItem != null)
                listBoxParentHelpItem.Items.RemoveAt(listBoxParentHelpItem.SelectedIndex);
        }

        public void LoadData(HelpItemManifestEntry entry)
        {
            if (entry.Parents != null)
            {
                listBoxParentHelpItem.Items.Clear();
                foreach (HelpItemManifestEntryParent p in entry.Parents)
                {
                    listBoxParentHelpItem.Items.Add(p);
                }
            }
        }
    }
}